<?php

namespace App\Repository\Information;

use App\Repository\BaseRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\UploadRepository;
use App\Models\InformationModel as model;
use App\Models\Information\InformationChannelModel;
use App\Models\GetWay\WayModel;
// use App\Api\Three\Distribution\HuLianWangFenXiao;
use App\Api\Transfer;


class InformationRepository extends BaseRepository
{
  protected $model;
  protected $informationChannelModel;
  protected $wayModel;
  public function __construct()
  {
    $this->model = new model();
    $this->informationChannelModel = new InformationChannelModel();
    $this->wayModel = new WayModel();
    parent::__construct();
  }
  public function index($where)
  {
    $cron = $where;
    if (empty($cron['order'])) {
      $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
    }
    $data = $this->search($this->model, $cron, 1, 10);
    return $data;
  }
  //查询可办理的业务
  public function queryMenu($data)
  {
    //接收用户提交的手机号
    $res = $this->informationChannelModel->where('page_id', $data['id'])->get()->toArray();
    //取出可用渠道id
    $channel_id = array_column($res, 'channel_id');
    //查询出可用渠道配置信息
    $channelData = $this->wayModel->whereIn('id', $channel_id)->get()->toArray();
    //要是没有可用通道返回报错
    if (!$channelData) {
      return $this->returnMsg(-1, '未查询到有可用渠道');
    }

    $model = new Transfer();

    foreach ($channelData as $v) {

      $channelValue = json_decode(json_encode($v), true);

      $channelConfigInfo = array_merge($data, $channelValue);

      $channelConfigInfo['model'] = $v['id'];
      
      $res = $model->queryMenu($channelConfigInfo);

      if ($res['code'] == 200) {

        return $this->returnMsg(200, '查询成功', $res);

        break;

      }
    }
    dump($res);
    exit;

    if ($res['code'] != 200) {

      return $this->returnMsg(-1, '查询失败');

    }

    return $res;
  }
  public function getSmsCode($data)
  {
    $data = [
      'mobile' => $data,
      'channel_id' => 245,
      'model' => 245,
    ];
    $where = [
      'id' => $data['channel_id'],
    ];
    $channelData = DB::table('way')->where($where)->first();
    if (!$channelData) {
      return $this->returnMsg(100, '未查询到该渠道');
    }
    $model = new Transfer();
    $channelData = json_decode(json_encode($channelData), true);
    $data = array_merge($data, $channelData);
    $res = $model->getSmsCode($data);
    return $res;
  }

  public function submitInfo($data)
  {
    $data = [
      'app_id' => 501295,
      //文档测试的
      'app_key' => '3888bd16498334490f5eaf4355be982f',
      //文档测试的

      'accesstokenUrl' => '111.7.112.55:20200/aopoauth/oauth/token',
      'url' => '111.7.112.55:20110/oppf',
      'grant_type' => 'client_credentials',
      'sourceId' => '1116',
    ];
    $model = new HuLianWangFenXiao($data);

    $res = $model->queryMenu($data);

    return $res;
  }

}